<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/index.css">
    <style>
        .container {
            background-image: url(./static/kuailefengnan.jpg);
            opacity: 0.8;
            z-index: -1;
            background-size: 510px 600px;
        }
    </style>
</head>

<body class="container shadow p-3 mb-5 bg-white rounded">

    <div class="color"></div>
    <div class="color"></div>
    <div class="color"></div>
    <div style="width: 42rem; height: 10rem; margin: auto;">
        <div class="card text-center">
            <div class="card-header">
                欢迎来到帅辉的微说说
            </div>
            <div class="card-body">
                <h5 class="card-title">当前用户：{{username}}</h5>
                <p class="card-text"></p>
                <div class="btn-group" role="group" aria-label="Basic example">
                    <button class="btn btn-secondary" data-toggle="modal" data-target="#saysomething"
                        type="button">发表说说</button>
                    <button type="button" class="btn btn-secondary" onclick="logout()">注销</button>
                </div>
            </div>
            <div id="datetime">
                <script>
                    setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
                </script>
            </div>
        </div>
        <div class="jumbotron" id="saysayMsg" style="width:42rem">
            {% for msg in msgs %}
            <div class="card border-primary shadow">
                <div class="card-header">
                    <div class="row">
                        <div class="col-sm-10">{{msg.content}}</div>
                        <div class="col-sm-2"><button type="button" class="btn btn-secondary btn-sm"
                                data-toggle="modal" data-target="#comment"
                                onclick="msgCommit('{{msg.id}}')">吐槽</button>
                        </div>
                    </div>

                </div>
            </div>
            <div style="background-color: rgba(134, 134, 134, 0.568);">
                <ul id="{{msg.id}}">
                    {% for cmot in msg.comments %}
                    <li class="list-group-item">
                        <blockquote class="blockquote mb-0">
                            <p>{{cmot.content}}</p>
                            <footer class="blockquote-footer">
                                <cite title="Source Title">{{cmot.comUserName}}</cite>&nbsp;说
                            </footer>
                        </blockquote>
                    </li>
                    {% endfor %}
                </ul>
            </div>
            {% endfor %}

        </div>

        <!--发表-->
        <div class="modal fade" id="saysomething" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">发表说说</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <input type="text" class="form-control" aria-describedby="passwordHelpInline"
                                    name="msgSay" id="msgSay">
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="say()">发表</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- 吐槽 -->
        <div class="modal fade" id="comment" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">吐槽一下</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <input type="text" class="form-control" aria-describedby="passwordHelpInline"
                                    id="commentText">
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary " data-dismiss="modal"
                            onclick="saveComment()">吐槽</button>
                    </div>
                </div>
            </div>
        </div>



        <script src="../static/js/jquery-3.6.0.min.js"></script>
        <script src="../static/js/bootstrap.min.js"></script>
        <script>
            let currentMsgId = '';
            function logout() {
                $.ajax({
                    url: "/logout",
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/login";
                        }
                    },
                    error: function (msg) {
                        console.log('网络或者服务器有问题');
                    }
                })
            }
            function say() {
                let data = {
                    msgSay: $('#msgSay').val()
                }
                $.ajax({
                    url: "/say",
                    type: "POST",
                    dataType: "json",
                    data: data,
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            let data = JSON.parse(res.data);
                            console.log(data);
                            let html = `

                            <div class="card border-primary shadow" style="width: 38rem;">
                            <div class="card-header">
                                <div class="row">
                                    <div class="col-sm-10">${data.content}</div>
                                    <div class="col-sm-2"><button type="button" class="btn btn-secondary btn-sm"
                                            data-toggle="modal" data-target="#comment"
                                            onclick="msgCommit(${data.id})">吐槽</button>
                                    </div>
                                </div>

                            </div>
                            <div class="card-body">
                                <ul class="list-group list-group-flush" id="${data.id}">
                                   
                                </ul>

                            </div>
                        </div>
                        `;



                            $("#saysayMsg").prepend(html);
                            // window.location.href="/login";
                        }
                    },
                    error: function (msg) {
                        console.log('网络或者服务器有问题');
                    }
                })
            }
            function msgCommit(msgId) {
                currentMsgId = msgId;
            }
            function saveComment() {

                let data = {
                    msgId: currentMsgId,
                    content: $('#commentText').val()
                }
                $.ajax({
                    url: "/comment",
                    type: "POST",
                    dataType: "json",
                    data: data,
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            let data = res.data;
                            console.log(data);
                            let html = `
                                        <li class="list-group-item">
                                            <blockquote class="blockquote mb-0">
                                                <p>${data.content}</p>
                                                <footer class="blockquote-footer">
                                                    <cite title="Source Title">${data.fromUserName}</cite>&nbsp;说
                                                </footer>

                                            </blockquote>
                                        </li>
                                         `;
                            $("#" + currentMsgId).append(html);
                            // window.location.href="/login";
                        }
                    },
                    error: function (msg) {
                        console.log('网络或者服务器有问题');
                    }
                });
            }
            function replaceDoc() {
                window.location.replace("/index")
            }


        </script>
</body>

</html>